package com.we823.project.controller.api;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.project.core.web.WebConstants;
import com.we823.project.model.repo.MaterialSafeModel;
import com.we823.project.model.repo.MaterialStockModel;
import com.we823.project.model.repo.MaterialStockOutModel;
import com.we823.project.model.repo.SafeAlertModel;
import com.we823.project.repository.service.repo.MaterialSafeService;
import com.we823.project.repository.service.repo.MaterialService;
import com.we823.project.repository.service.repo.MaterialStockOutService;
import com.we823.project.repository.service.repo.MaterialStockService;

@Controller
@RequestMapping(WebConstants.API_STOCK_ALERT)
public class StockAlertController {
	
	private MaterialSafeService materialSafeService;
	private MaterialService materialService;
	private MaterialStockService materialStockService;
	
	@Autowired
	private MaterialStockOutService materialStockOutService;
	
	@Autowired
	public StockAlertController(MaterialSafeService materialSafeService, MaterialService materialService, MaterialStockService materialStockService) {
		this.materialSafeService = materialSafeService;
		this.materialService = materialService;
		this.materialStockService = materialStockService;
	}


	@RequestMapping("check_safe")
	@ResponseBody
	public Model checkSafe(HttpServletRequest request,Model model){
		List<MaterialSafeModel> materialSafeModels = materialSafeService.findBySafenumIsNotNull();
		List<SafeAlertModel> safeAlertModels = null;
		if(materialSafeModels!=null){

			List<String> materialCodeList= new ArrayList<>();
			Map<String, MaterialSafeModel> safeMap = new HashMap<>();
			for (MaterialSafeModel materialSafeModel : materialSafeModels) {
				materialCodeList.add(materialSafeModel.getCode());
				safeMap.put(materialSafeModel.getCode(), materialSafeModel);
			}
			
			if(materialCodeList!=null && materialCodeList.size()>0){
				Calendar calendar = Calendar.getInstance();
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH)+1;
				
				Sort sort = new Sort(Direction.DESC,"id");
				List<MaterialStockModel> materialStockModels = materialStockService.findByYearAndMonthAndMaterialCodeIn(year,month,materialCodeList,sort);
				if(materialStockModels!=null){
					Map<String, MaterialStockModel> stockMap = new HashMap<>();
					for (MaterialStockModel materialStockModel : materialStockModels) {
						String materialCode = materialStockModel.getMaterialCode();
						if(!stockMap.containsKey(materialCode)){
							stockMap.put(materialCode, materialStockModel);
						}
					}
					
					if(stockMap!=null && stockMap.size()>0){
						Set<String> codeSet = stockMap.keySet();
						for (String code : codeSet) {
							MaterialStockModel materialStockModel = stockMap.get(code);
							MaterialSafeModel materialSafeModel = safeMap.get(code);
							
							if(materialSafeModel!=null && materialStockModel!=null){
								
								safeAlertModels = new ArrayList<>();
								
								if(materialSafeModel.getSafenum() > materialStockModel.getStock()){
									SafeAlertModel safeAlert = new SafeAlertModel();
									safeAlert.setCode(code);
									safeAlert.setName(materialSafeModel.getFullname());
									safeAlert.setTotal(materialStockModel.getStock());
									safeAlert.setSafenum(materialSafeModel.getSafenum());
									
									safeAlertModels.add(safeAlert);
								}
							}
						}
					}
				}
			}
		}
		model.addAttribute("safeAlerts",safeAlertModels);
		
		
		
		List<MaterialStockOutModel> materialStockOutModels = materialStockOutService.findByFlag(0);
		if(materialStockOutModels!=null) {
			int size = materialStockOutModels.size();
			if(size>0) {
				model.addAttribute("needOut",true);
				model.addAttribute("outSize",size);
			}else {
				model.addAttribute("needOut",false);
				model.addAttribute("outSize",0);
			}
		}
		return model;
	}


	public MaterialStockOutService getMaterialStockOutService() {
		return materialStockOutService;
	}


	public void setMaterialStockOutService(
			MaterialStockOutService materialStockOutService) {
		this.materialStockOutService = materialStockOutService;
	}
	
	
}
